using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using CodePlex.Resourceful;

namespace CodePlex.Resourceful.Amazon.S3
{
    public class ACL
    {
        private readonly ACLPrincipal _owner;
        private readonly Dictionary<string, List<ACLPrincipal>> _permissions;
        private readonly Converter<string, string> _descriptions;
        private readonly bool _isReadOnly;

        public ACL(bool isReadOnly, ACLPrincipal owner, Converter<string, string> descriptions)
        {
            _isReadOnly = isReadOnly;
            _owner = owner;
            _descriptions = descriptions;
            _permissions = new Dictionary<string, List<ACLPrincipal>>();
        }

        public bool IsReadOnly
        {
            get
            {
                return _isReadOnly;
            }
        }

        public ACLPrincipal Owner
        {
            get
            {
                return _owner;  
            }
        }

        public ReadOnlyList<ACLPrincipal> Principals
        {
            get
            {
                List<ACLPrincipal> rt = new List<ACLPrincipal>();

                foreach (string permission in _permissions.Keys)
                {
                    foreach (ACLPrincipal principal in _permissions[permission])
                    {
                        if (!rt.Contains(principal))
                        {
                            rt.Add(principal);
                        }
                    }
                }

                return new ReadOnlyList<ACLPrincipal>( rt);
            }
        }

        public string GetDescription(string permission)
        {
            if (_descriptions == null)
            {
                return string.Empty;
            }
            return _descriptions(permission);
        }

        public ReadOnlyList<string> GetPermissions(ACLPrincipal principal)
        {
            List<string> rt = new List<string>();

            foreach (string permission in _permissions.Keys)
            {
                if (_permissions[permission].Contains(principal))
                {
                    rt.Add(permission);
                }
            }

            return new ReadOnlyList<string>(rt);
        }

        public void Add(ACLPrincipal principal, string permission)
        {
            if (permission == null)
            {
                return;
            }
            if (!_permissions.ContainsKey(permission))
            {
                _permissions.Add(permission, new List<ACLPrincipal>());
            }

            _permissions[permission].Add(principal);
          
        }

        public void Remove(ACLPrincipal principal, string permission)
        {
            if (!_permissions.ContainsKey(permission))
            {
                return;
            }

            if (_permissions[permission].Contains(principal))
            {
                _permissions[permission].Remove(principal);
            }
        }


    }




}
